package com.woniuxy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.entity.Address;
import com.woniuxy.provider.AddressProvider;
import com.woniuxy.provider.UserProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 收货地址Mapper接口
 */
public interface AddressMapper extends BaseMapper<Address> {
    @Select("select*from gfr_address where user_id=#{userid}")
    @Results({
            @Result(column = "user_id",property = "userid"),
            @Result(column = "addr_phone",property = "phone"),
            @Result(column = "addr_province",property = "province"),
            @Result(column = "addr_city",property = "city"),
            @Result(column = "addr_district",property = "district"),
            @Result(column = "address",property = "address"),
            @Result(column = "addr_status",property = "status"),
    })
    public List<Address> getByAddr(int userid);

    @Select("select*from gfr_address where id=#{id}")
    @Results({
            @Result(column = "user_id",property = "userid"),
            @Result(column = "addr_phone",property = "phone"),
            @Result(column = "addr_province",property = "province"),
            @Result(column = "addr_city",property = "city"),
            @Result(column = "addr_district",property = "district"),
            @Result(column = "address",property = "address"),
            @Result(column = "addr_status",property = "status"),
    })
    public Address getByIdAddr(int id);

    /**
     * @Author Mr.Cai
     * 通过用户id查询当前用户已经拥有的收货地址数量
     * @param id
     * @return
     */
    @Select("select count(*) from gfr_address where user_id = #{userid}")
    public int getCountByUserId(int id);

    /**
     * 通过地址id修改地址
     */
    @UpdateProvider(type = AddressProvider.class,method = "update")
    @Results({
            @Result(column = "addr_phone",property = "phone"),
            @Result(column = "addr_province",property = "province"),
            @Result(column = "addr_city",property = "city"),
            @Result(column = "addr_district",property = "district"),
            @Result(column = "address",property = "address"),
            @Result(column = "addr_status",property = "status"),
    })
     void updateByAddId(Address address);

    @Select("select * from gfr_address where addr_status=#{status} and user_id=#{userId}")
    Address getDefault(int userId,String status);

    /**
     * 通过用户id查找该用户拥有多少收货地址
     * @param uesrid
     * @return
     */
    @Select("select count(*) from gfr_address where user_id = #{userid}")
    int getAddCount(int uesrid);

}
